<?php
class Showcase{
    private $connection;
    private $datas;
    
    /**
     * Empty costructor
     */
    public function __construct() {
        $this->connection = Yii::app()->db;
        $this->datas = array();
    }

    /**
     * Rows of hitcounter's query
     */
    private function hitCounterRows(){
        $sql = "SELECT DISTINCT ip, DATE_FORMAT(date, '%Y') AS 'date', page FROM hitcounter ORDER BY date;";
        $command = $this->connection->createCommand($sql);
        return $command->queryAll();
    }
    
    /**
     * Numbero of visited pages
     */
    private function getCountYearPage(){
        //Conto le pagine visitate nei vari anni
        $pagecount = 1;
        $ipcount = 1;
        $totalcountpage = 0;
        $rows = $this->hitCounterRows();
        
        foreach ($rows as $key => $value){
            //echo $value['date']."<br>";
            if(($key+1)<  count($rows)){
                if($rows[$key+1]['date'] == $value['date']){
                    $pagecount ++;
                }
                $this->datas['countpage'][$value['date']] = $pagecount;
            }
            $totalcountpage ++;
            $this->datas['date'][] = $value['date'];
        }
        $this->datas['totalcountpage'] = $totalcountpage;
    }
    
    /**
     * Total visitors of site
     */
    public function getTotalVisitors(){
        $rows = $this->hitCounterRows();
        $ip = array();
        //Conto le visite totali
        foreach ($rows as $key => $value){
            //Creo l'array degli ip
            $ip[] = $value['ip'];
        }
        //Elimino i duplicati
        $ip = array_unique($ip);
        $this->datas['ipuniquecount'] = count($ip);//Conto gli ip che hanno visitato la pagina
        
    }
    
    /**
     * Site's visitors
     */
    public function getVisitors(){
        $sql = "SELECT COUNT(*) AS 'count' FROM hitcounter;" ;
        $command = $this->connection->createCommand($sql);
        $rows = $command->queryAll();
        $this->datas['ipcount'] = $rows[0]['count'];
    }

    /**
     * Calculates value of count page
     */
    public function getValuesCountPage(){
        $valuesPageCount = '';
        /*$this->datas['values'] = array();
        $this->datas['values']['countpage'] = array();*/
        if(isset($this->datas['countpage'])){
            foreach ($this->datas['countpage'] as $i => $value){
                $valuesPageCount .= '[\''.$i.'\','.$value.'], ';
            }
            $this->datas['values']['countpage'] = $valuesPageCount;
        }else{
            $this->datas['values'] = '';
            $this->datas['values']['countpage'] = '';
        }
    }
    
    /**
     * Values of ip counter
     */
    public function getValuesIPCounter(){
        $valuesIpCounter = '';
        if(isset($this->datas['ipcounter'])){
            foreach ($this->datas['ipcounter'] as $key => $value){
                $valuesIpCounter .= '[\''.$key.'\', '.$value.'], ';
            }
            $this->datas['values']['ipcounter'] = $valuesIpCounter;
        }else{
            $this->datas['values']['ipcounter'] = 0;
        }
    }

    /**
     * IP counter
     */
    public function getIPCount(){
        $sql = "SELECT COUNT(date) AS 'countdate', DATE_FORMAT(date, '%Y') AS 'date' FROM `hitcounter` GROUP BY DATE_FORMAT(date, '%Y');";
        $command = $this->connection->createCommand($sql);
        $ipcounter = $command->queryAll();
        foreach ($ipcounter as $key => $value){
            $this->datas['ipcounter'][$value['date']] = $value['countdate'];
        }
    }
    
    /**
     * Number of users
     */
    public function getCountUsers(){
        $sql = "SELECT COUNT(*) AS 'count' FROM users;";
        $command = $this->connection->createCommand($sql);
        $rows = $command->queryAll();
        
        $this->datas['site']['users']['count'] = $rows[0]['count'];
    }

    /**
     * Number of downloads
     */
    public function getCountDownloads($name){
        $sql = "SELECT counter FROM downloads WHERE name = '$name'";
        $command = $this->connection->createCommand($sql);
        $rows = $command->queryAll();
        
        if(isset($rows[0])){
            $this->datas['site']['downloads']['count'] = $rows[0]['counter'];
        }else{
            $this->datas['site']['downloads']['count'] = 0;
        }
    }

    /**
     * Create array of datas
     */
    public function createDatas(){
        $this->getCountYearPage();
        $this->getTotalVisitors();
        $this->getValuesCountPage();
        $this->getVisitors();
        $this->getIPCount();
        $this->getValuesIPCounter();
        $this->getCountUsers();
        $this->getCountDownloads('lecturenotes_android.apk');
    }

    /**
     * Return HTML for comment module
     */
    public function getUsersHTML(){
        echo "\t<div class='sitecomment'>\n";
            echo "\t\t<div class='page'>\n";
                echo "\t\t\t<div class='container'>\n";
                    echo "\t\t\t\t<div class='name'>Utenti iscritti: </div>\n";
                    echo "\t\t\t\t<div class='value'>".$this->datas['site']['users']['count']."</div>";
                    echo "\t\t\t\t<div class='link'>\n";
                        echo "\t\t\t\t\t<div>".CHtml::link('Nuovo', array('/users/create'), array('target'=>'_blank'))."</div>\n";
                        echo "\t\t\t\t\t<div>".CHtml::link('Gestisci', array('/users/admin'), array('target'=>'_blank'))."</div>\n";
                        echo "\t\t\t\t\t<div>".CHtml::link('Show', array('/users'), array('target'=>'_blank'))."</div>\n";
                    echo "\t\t\t\t</div>\n";
                echo "\t\t\t</div>\n";
            echo "\t\t</div>\n";
        echo "\t</div>\n";
    }
    
    /**
     * Return HTML for downloads module
     */
    public function getDownloadsHTML(){
        echo "\t<div class='sitecomment'>\n";
            echo "\t\t<div class='page'>\n";
                echo "\t\t\t<div class='container'>\n";
                    echo "\t\t\t\t<div class='name'>Download effetuati per \"<i>Lecture Notes for Android</i>\": </div>\n";
                    echo "\t\t\t\t<div class='value'>".$this->datas['site']['downloads']['count']."</div>";
                echo "\t\t\t</div>\n";
            echo "\t\t</div>\n";
        echo "\t</div>\n";
    }

    /**
     * Return HTML for hitcounter module
     */
    public function getHitCounterHTML(){
        echo "\t<div class=\"hitcounter\">\n";
            echo "\t\t<div class='page'>\n";
                echo "\t\t\t<div class='container'>\n";
                    echo "\t\t\t\t<div class='name'>Pagine visitate</div>\n";
                    echo "\t\t\t\t<div class='value'>".$this->datas['totalcountpage']."</div>";
                    echo "\t\t\t\t<div class='link'>".CHtml::link('Grafico', array('graphics/hitcounterpage/datas/'.$this->datas['values']['countpage']), array('target'=>'_blank'))."</div>\n";
                echo "\t\t\t</div>\n";

                echo "\t\t\t<div class='container'>\n";
                    echo "\t\t\t\t<div class='name'>Visitatori</div>\n";
                    echo "\t\t\t\t<div class='value'>".$this->datas['ipcount']."</div>\n";
                echo "\t\t\t</div>\n";

                echo "\t\t\t<div class='container'>\n";
                    echo "\t\t\t\t<div class='name'>Visitatori unici</div>\n";
                    echo "\t\t\t\t<div class='value'>".$this->datas['ipuniquecount']."</div>\n";
                    echo "\t\t\t\t<div class='link'>".CHtml::link('Grafico', array('graphics/hitcounterip/datas/'.$this->datas['values']['ipcounter']), array('target'=>'_blank'))."</div>\n";
                echo "\t\t\t</div>\n";
            echo "\t\t</div>\n";
        echo "\t</div>\n";
    }
    
    /**
     * Write showcase
     */
    public function getAll(){
        $this->createDatas();//Create datas
        
        echo "<div id=\"showcase\">\n";
        $this->getHitCounterHTML();
        $this->getUsersHTML();
        $this->getDownloadsHTML();
        echo "</div>\n";
    }
}